package com.tjakus.arduino.bean;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.primefaces.model.chart.MeterGaugeChartModel;
import com.tjakus.arduino.hibernate.HibernateUtil;
import com.tjakus.arduino.hibernate.Sensors;
import com.tjakus.arduino.logging.Logging;


public class SensorsBean {

	private List<Sensors> sensorsList;
	
	public SensorsBean() {
		List<Sensors> sensorsListNoWithGaugeChart = HibernateUtil.getAllSensors();
		List<Sensors> sensorListWithGaugeChart = new ArrayList<Sensors>();
		for (Iterator<Sensors> iter = sensorsListNoWithGaugeChart.iterator(); iter
				.hasNext();) {
			Sensors sensor = iter.next();
			List<Number> interval = new ArrayList<Number>();
			if (sensor.getType().equalsIgnoreCase("TMP")) {
				interval.add(15);
				interval.add(30);
				interval.add(50);
			}
			else if (sensor.getType().equalsIgnoreCase("LGT")){
				interval.add(200);
				interval.add(600);
				interval.add(1000);
			}
			else {
				interval.add(0.5);
				interval.add(1);
			}
			NumberFormat format = NumberFormat.getInstance(Locale.US);
			Number number = 0;
			try {
				number = format.parse(sensor.getValue());
				Logging.getLogger().debug("SensorsBean - number [ " + number + " ]");
				Logging.getLogger().debug("SensorsBean - interval [ " + interval + " ]");
			} catch (ParseException e) {
				Logging.getLogger().error("SensorsBean - UNABLE TO PARS NUMBER!!!", e);
			}
			MeterGaugeChartModel meterGaugeModel = new MeterGaugeChartModel(number, interval);
			sensor.setMeterGaugeChartModel(meterGaugeModel);
			sensorListWithGaugeChart.add(sensor);
		}
		this.setSensorsList(sensorListWithGaugeChart);
	}
	
	public List<Sensors> getSensorsList() {
		Logging.getLogger().info("SensorsBean - getSensorsList [ ]");
		return sensorsList;
	}

	public void setSensorsList(List<Sensors> newSensorsList) {
		this.sensorsList = newSensorsList;
	}

}
